/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.service.export.xls;

import java.util.List;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import org.tnmk.worktime.config.AppConfig;
import org.tnmk.worktime.service.SessionService;
import org.tnmk.worktime.service.dao.entry.SessionEntry;
import org.tnmk.worktime.service.dao.entry.SpendEntry;
import org.tnmk.worktime.utils.NumUtils;

/**
 *
 * @author tnmkhoi
 */
public class SpendExport extends ExcelExporter<List<SpendEntry>> {
  private final SessionService service;
  private static final int ROW_HEADER = 1;
  private static final int COL_SESSION_INTIME = 0;
  private static final int COL_NOTE = 1;
  private static final int COL_COST = 2;
  private static final int COL_SPEND_ID = 3;
  
  public SpendExport(SessionService service){
    this.service = service;
  }
  
  @Override
  protected void export(WritableSheet s, List<SpendEntry> data) throws WriteException {
    s.setName("Spend Analysis");

    //Header
    exportHeader(s, "Session", "Note", "Cost", "Id");
    
    //Export Body
    exportSpend(s, data);
    
    
  }

  protected void exportHeader(WritableSheet s, String... headers) throws WriteException {
    // Create cell font and format
    WritableFont cellFont = new WritableFont(WritableFont.COURIER, 12);
    cellFont.setBoldStyle(WritableFont.BOLD);
    WritableCellFormat cellFormat = new WritableCellFormat(cellFont);

    for (int col = 0; col < headers.length; col++) {
      String header = headers[col];
      s.addCell(new Label(col, ROW_HEADER, header, cellFormat));
    }
  }
  protected void exportSpend(WritableSheet s, List<SpendEntry> spends) throws WriteException {
    int initRow = 3;
    int row = initRow;
    for (SpendEntry spend : spends) {
      exportSpend(s, spend, row);
      row++;
    }
  }
  protected void exportSpend(WritableSheet s, SpendEntry spend, int row) throws WriteException {
    SessionEntry session = service.findSession(spend.getSessionId());
    String inTime = AppConfig.toString(session.getInTime());
    
    s.addCell(new Label(COL_COST, row, NumUtils.toString(spend.getCost())));
    s.addCell(new Label(COL_NOTE, row, spend.getNote()));
    s.addCell(new Label(COL_SPEND_ID, row, spend.getId().toString()));
    s.addCell(new Label(COL_SESSION_INTIME, row, inTime));
  }
}
